package com.butnet.myframe.hql.query;

import org.hibernate.Query;

/**
 * Where 条件
 * 
 * @author Butnet
 */
public class Where {
	private static Object syncObject = new Object();
	private static int argNumber = 0;

	/**
	 * @return 取得唯一的参数名称
	 */
	public static String argName() {
		synchronized (syncObject) {
			argNumber++;
			return "arg" + argNumber;
		}
	}

	private final String argName;
	private String objAsName = null;
	private String colName = null;
	private String expression = null;
	private Object value = null;

	public Where() {
		synchronized (syncObject) {
			argNumber++;
			argName = "arg" + argNumber;
		}
	}

	public Where(String colName, String exp, Object value) {
		this();
		setColName(colName);
		setExpression(exp);
		setValue(value);
	}

	public String getObjAsName() {
		return objAsName;
	}

	public void setObjAsName(String objAsName) {
		this.objAsName = objAsName;
	}

	public String getArgName() {
		return argName;
	}

	public String getColName() {
		return colName;
	}

	public void setColName(String colName) {
		this.colName = colName;
	}

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	@Override
	public String toString() {
		if(value!=null)
			return "( " + objAsName + "." + colName + " " + expression + " :" + argName + " )";
		else
			return "( " + objAsName + "." + colName + " " + expression + " )";
	}

	public void setParameters(Query q) {
		if(value!=null)
			q.setParameter(argName, value);
	}
}
